<template>
	<view class="wrap">
		<view class="notice">
			<u-notice-bar mode="horizontal" :list="list" type="none" :color="colors"></u-notice-bar>
		</view>
		<view class="wrap_content">
			<view class="title">
				<view class="time">{{$u.timeFormat(Date.now(), 'mm月dd日 hh:MM')}} | {{fontNum}} 字</view>
				<view class="showSuc" @click="publishBook" :style="{'color' : colors,'display':display}">
					发布
				</view>
			</view>
			<view class="type">发布类型：<text class="typeItem" v-for="(item,index) in publishType" :key="index"
					@click="selectPublishType(item,index)"
					:style="active === index ? 'color:#F3C301' : ''">{{item}}</text>
			</view>
			<view class="bookName">
				<input type="text" placeholder="发布书名" v-model="bookName" style="font-weight: bold; font-size: 35rpx;" />
			</view>
			<view class="content">
				<textarea v-model="publishContent" maxlength="-1" auto-height style="width: 100%;"
					placeholder="畅谈你的看法吧..." :show-confirm-bar="false" @input="getFontNum"></textarea>
			</view>
		</view>
	</view>
</template>

<script>
	import * as cloudApi from '@/utils/cloudApi.js'
	export default {
		data() {
			return {
				fontNum: 0,
				list: ['严禁盗取、复制别人的内容，一经发现拉入黑名单'],
				display: 'none',
				userInfo: {},
				token: null,
				publishType: ['推书', '扫书', '排雷', '吐槽'],
				bookName: '',
				publishContent: '',
				colors: '',
				// 判断选中哪个高亮哪个
				active: 0,
				typeContent: '推书'
			}
		},

		onLoad() {
			this.userInfo = uni.getStorageSync("userInfo") || getApp().globalData.userInfo
			this.token = uni.getStorageSync("token") || getApp().globalData.token
			this.colors = this.userInfo.themeColors || '#F3C301'
		},
		methods: {
			selectPublishType(item, index) {
				this.active = index;
				console.log(item);
				this.typeContent = item
			},
			getFontNum(e) {
				console.log(e);
				this.fontNum = e.detail.value.length
			},
			publishBook() {
				if (this.typeContent == '吐槽') {
					let bn = this.bookName
				} else {
					let bn = '《' + this.bookName + '》'
				}

				cloudApi.call({
					name: "book",
					data: {
						api: 'publishBook',
						content: this.publishContent,
						bookname: bn,
						word: this.fontNum,
						type: this.typeContent,
						nickName: this.userInfo.nickName,
						token: this.token
					},
					success: (res) => {
						console.log(res);
						this.$showToast('发布成功', 'none')
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							})
						}, 1000)
					},
					fail: err => {
						this.$showToast('发布失败', 'warring')
					}
				})
			}
		},
		onReady() {
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: this.colors
			})
		},
		watch: {
			//无内容时按钮置灰
			publishContent(value) {
				value ? (this.display = '') : (this.display = 'none');
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ffffff;
	}

	.wrap {
		.notice {
			width: 100%;
			height: 80rpx;
		}

		.wrap_content {
			padding: 10rpx 20rpx 40rpx;

			.title {
				display: flex;
				justify-content: space-between;
				font-size: 26rpx;
				margin-bottom: 20rpx;

				.time {
					color: #ccc;
				}

				.showSuc {
					padding: 0 20rpx;
					font-weight: bold;
				}
			}

			.type {
				padding: 20rpx 0;

				.active {
					background: #ffffff;
					color: rgb(42, 183, 149);
				}

				.typeItem {
					padding: 15rpx 40rpx;
					font-size: 24rpx;
					background-color: #F2f2f4;
					text-align: center;
					border-radius: 25rpx;
					margin-right: 10rpx;
				}
			}

			.bookName {
				border-top: 1rpx solid #F8f8f9;
				margin: 20rpx 0;
				border-bottom: 1rpx solid #F8f8f9;
				padding: 20rpx 0;
				color: #000000;
			}

			.content {}
		}
	}
</style>
